import { getArtCateListAPI, getUserInfoApi } from '@/api'
import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    token: '',
    userInfo: {}, // 保存用户信息
    artCateList: {}// 保存文章分类信息
  },
  getters: {
    // username (state) {
    //   return state.userInfo.username
    // },
    // username: (state) => {
    //   return state.userInfo.username
    // }
    username: state => state.userInfo.username,
    nickname: state => state.userInfo.nickname,
    user_pic: state => state.userInfo.user_pic
  },
  mutations: {
    updateToken (state, val) {
      state.token = val
    },
    // 保存用户的信息
    updateUserInfo (state, info) {
      state.userInfo = info
    },
    // 保存文章分类信息
    updateArtCateList (state, val) {
      state.artCateList = val
    }
  },
  actions: {
    // 请求用户信息
    async getUserInfoAction (store) {
      const res = await getUserInfoApi()
      // console.log(res)
      store.commit('updateUserInfo', res.data.data)
    },
    async getArtCateList (store) {
      const res = await getArtCateListAPI()
      store.commit('updateArtCateList', res.data.data)
    }
  },
  modules: {
  },
  // 插件
  plugins: [
    createPersistedState()// 注入持久化插件
  ]
})
